﻿using System;
using Abp.Dependency;
using Castle.Core.Logging;
using Hangfire;
using KelonProud.Extensions;

namespace KelonProud.BackgroundJobs.Jobs.Base
{
    public abstract class CronJobBase: ITransientDependency
    {
        public ILogger Logger { protected get; set; }

        /// <summary>
        /// 时间表达式
        /// </summary>
        public abstract string CronExpression { get; }

        public abstract void Execute();

        public virtual void Init()
        {
            if (!CronExpression.HasValue())
            {
                throw new Exception("时间表达式必须填写");
            }

            try
            {
                RecurringJob.AddOrUpdate(() => Execute(), CronExpression);
            }
            catch (Exception e)
            {
               Logger.Debug(e.GetTrueMessage());
                throw;
            }
        }
    }
}
